// 模拟提现API数据
const mockWithdrawConfig = {
  minAmount: 10,
  maxAmount: 1000,
  dailyLimit: 2,
  fee: {
    under100: 2,
    over100: 0
  }
}

const mockWithdrawRecords = [
  {
    _id: 'withdraw_001',
    userId: 'user_001',
    amount: 100,
    fee: 0,
    actualAmount: 100,
    withdrawMethod: 'wechat',
    status: 'success',
    auditStatus: 'approved',
    accountInfo: {
      accountName: '张三',
      accountNumber: 'oUpF8uMuAJO_M2pxb1Q9zNjWeS6o'
    },
    createdAt: '2024-01-15T10:30:00.000Z',
    processedAt: '2024-01-15T11:00:00.000Z',
    wechatInfo: {
      outBatchNo: 'TB202401150001',
      batchId: 'batch_202401150001',
      outDetailNo: 'WDwithdraw_001'
    }
  },
  {
    _id: 'withdraw_002',
    userId: 'user_001',
    amount: 50,
    fee: 2,
    actualAmount: 48,
    withdrawMethod: 'wechat',
    status: 'pending',
    auditStatus: 'pending',
    accountInfo: {
      accountName: '张三',
      accountNumber: 'oUpF8uMuAJO_M2pxb1Q9zNjWeS6o'
    },
    createdAt: '2024-01-16T14:20:00.000Z'
  },
  {
    _id: 'withdraw_003',
    userId: 'user_001',
    amount: 200,
    fee: 0,
    actualAmount: 200,
    withdrawMethod: 'wechat',
    status: 'processing',
    auditStatus: 'approved',
    accountInfo: {
      accountName: '张三',
      accountNumber: 'oUpF8uMuAJO_M2pxb1Q9zNjWeS6o'
    },
    createdAt: '2024-01-16T16:45:00.000Z',
    processedAt: '2024-01-16T17:00:00.000Z'
  },
  {
    _id: 'withdraw_004',
    userId: 'user_001',
    amount: 80,
    fee: 2,
    actualAmount: 78,
    withdrawMethod: 'wechat',
    status: 'failed',
    auditStatus: 'rejected',
    accountInfo: {
      accountName: '张三',
      accountNumber: 'oUpF8uMuAJO_M2pxb1Q9zNjWeS6o'
    },
    createdAt: '2024-01-14T09:15:00.000Z',
    processedAt: '2024-01-14T10:00:00.000Z',
    remark: '账户信息有误'
  }
]

const mockStats = {
  counts: {
    pending: 1,
    processing: 1,
    success: 1,
    failed: 1,
    total: 4
  },
  amounts: {
    totalAmount: 430.00,
    successAmount: 100.00
  }
}

// 模拟API响应
function createMockResponse(success, data, message = '') {
  return {
    success,
    data,
    message: message || (success ? '操作成功' : '操作失败')
  }
}

// 模拟延迟
function delay(ms = 1000) {
  return new Promise(resolve => setTimeout(resolve, ms))
}

// 模拟API函数
export const mockWithdrawAPI = {
  // 获取提现配置
  async getWithdrawConfig() {
    await delay(500)
    return createMockResponse(true, mockWithdrawConfig)
  },

  // 申请提现
  async applyWithdraw(data) {
    await delay(1500)
    
    // 模拟验证
    if (!data.amount || data.amount < 10 || data.amount > 1000) {
      return createMockResponse(false, null, '提现金额不符合要求')
    }
    
    if (!data.accountInfo || !data.accountInfo.accountNumber) {
      return createMockResponse(false, null, '账户信息不完整')
    }
    
    // 模拟创建提现记录
    const newRecord = {
      _id: `withdraw_${Date.now()}`,
      userId: 'user_001',
      amount: data.amount,
      fee: data.amount >= 100 ? 0 : 2,
      actualAmount: data.amount - (data.amount >= 100 ? 0 : 2),
      withdrawMethod: data.withdrawMethod,
      status: 'pending',
      auditStatus: 'pending',
      accountInfo: data.accountInfo,
      createdAt: new Date().toISOString()
    }
    
    mockWithdrawRecords.unshift(newRecord)
    
    return createMockResponse(true, newRecord, '提现申请提交成功')
  },

  // 获取提现记录列表
  async getWithdrawList(params = {}) {
    await delay(800)
    
    let records = [...mockWithdrawRecords]
    
    // 状态筛选
    if (params.status) {
      records = records.filter(record => record.status === params.status)
    }
    
    // 分页
    const page = params.page || 1
    const limit = params.limit || 10
    const start = (page - 1) * limit
    const end = start + limit
    const paginatedRecords = records.slice(start, end)
    
    return createMockResponse(true, {
      records: paginatedRecords,
      total: records.length,
      page,
      limit
    })
  },

  // 获取提现详情
  async getWithdrawDetail(withdrawId) {
    await delay(500)
    
    const record = mockWithdrawRecords.find(r => r._id === withdrawId)
    if (!record) {
      return createMockResponse(false, null, '提现记录不存在')
    }
    
    return createMockResponse(true, record)
  },

  // 查询微信提现状态
  async queryWechatWithdrawStatus(withdrawId) {
    await delay(800)
    
    const record = mockWithdrawRecords.find(r => r._id === withdrawId)
    if (!record) {
      return createMockResponse(false, null, '提现记录不存在')
    }
    
    // 模拟微信状态查询
    const wechatStatus = {
      batch_id: record.wechatInfo?.batchId || 'batch_mock',
      out_batch_no: record.wechatInfo?.outBatchNo || 'TB_mock',
      batch_status: record.status === 'success' ? 'SUCCESS' : 
                   record.status === 'failed' ? 'FAILED' : 'PROCESSING',
      batch_type: 'API',
      batch_name: '佣金提现',
      batch_remark: '惠邻生活佣金提现',
      total_amount: record.amount * 100, // 微信金额单位为分
      total_num: 1,
      success_amount: record.status === 'success' ? record.amount * 100 : 0,
      success_num: record.status === 'success' ? 1 : 0,
      fail_amount: record.status === 'failed' ? record.amount * 100 : 0,
      fail_num: record.status === 'failed' ? 1 : 0
    }
    
    return createMockResponse(true, {
      withdrawRecord: record,
      wechatStatus
    })
  },

  // 获取提现统计
  async getWithdrawStats() {
    await delay(600)
    return createMockResponse(true, mockStats)
  },

  // 重新申请提现
  async retryWithdraw(withdrawId) {
    await delay(1000)
    
    const record = mockWithdrawRecords.find(r => r._id === withdrawId)
    if (!record) {
      return createMockResponse(false, null, '提现记录不存在')
    }
    
    if (record.status !== 'failed') {
      return createMockResponse(false, null, '只能重新申请失败的提现')
    }
    
    // 更新状态为待审核
    record.status = 'pending'
    record.auditStatus = 'pending'
    record.processedAt = null
    record.remark = null
    
    return createMockResponse(true, record, '重新申请成功')
  }
} 